package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-20 20:43
 * @description：
 */
@Controller
@ResponseBody
@RequestMapping("/house")
public class HouseController {
    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private AdminService adminService;
    @DubboReference
    private UserFollowService userFollowService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result findList(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                           @RequestBody HouseQueryVo houseQueryVo) {
        PageInfo<HouseVo> list = houseService.getList(pageNum, pageSize, houseQueryVo);
        return Result.ok(list);
    }

    @RequestMapping("/info/{houseId}")
    public Result infoShow(@PathVariable Long houseId, HttpSession session) {
        House house = houseService.findById(houseId);
        Community community = communityService.findById(house.getCommunityId());
        List<HouseBroker> brokers = houseBrokerService.findBrokersByHouseId(houseId);
        // 由于可能会出现一个房源没有代理人的情况，这时我们就要给他设置一个默认代理人
        if (brokers == null || brokers.size() == 0) {
            //设置id为1的代理人为默认代理人
            Admin admin = adminService.findById(1);
            HouseBroker houseBroker = new HouseBroker();
            houseBroker.setHouseId(houseId);
            houseBroker.setBrokerId(admin.getId());
            houseBroker.setBrokerName(admin.getName());
            houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
            brokers.add(houseBroker);
        }
        List<HouseImage> image = houseImageService.findImage(houseId, 1);
        Boolean isFollow = false;
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if (userInfo != null) {
            UserFollow user = userFollowService.findUser(houseId, userInfo.getId());
            if (user!=null) {
                isFollow = true;
            }
        }
        Map map = new HashMap<>();
        map.put("house", house);
        map.put("community", community);
        map.put("houseImage1List", image);
        map.put("houseBrokerList", brokers);
        map.put("isFollow", isFollow);
        return Result.ok(map);
    }
}
